package com.acertificate.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class HtmlParserUtil {
	
	
	public static final String URL_FROM = "http://x.wanmv.com/article/list_5_1.html";
	public static final String URL_TO = "http://x.wanmv.com/article/list_5_379.html";
	
	public static final String DTD = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\">";

	public static void main(String[] args) throws ParserException, MalformedURLException, IOException {
		HtmlParserUtil hpu = new HtmlParserUtil();
		URLConnection urlConnection = new URL("http://x.wanmv.com/article/list_5_1.html").openConnection();
//		String content = IOUtils.toString(urlConnection.getInputStream(), "gbk");
		Parser parser = new Parser(urlConnection);
//		parser.setInputHTML(content);
		AndFilter andFilter = new AndFilter(new NodeClassFilter(LinkTag.class), new HasAttributeFilter("class","title"));
		NodeList list = parser.extractAllNodesThatMatch(andFilter);
		
		for (int i = 0; i < list.size(); i++) {
			Node tag = list.elementAt(i);
			System.out.println(tag.toHtml());
		}
	}

	Parser contentParser = new Parser();

	public String getContent(String url) throws ParserException {
		contentParser.setURL(url);
		AndFilter filter = new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("id", "Cnt-Main-Article-QQ"));
		NodeList nodes = contentParser.parse(filter);
		for (int i = 0; i < nodes.size(); i++) {
			Node node = nodes.elementAt(i);
			System.out.println("\t" + node.toHtml().replace("\n", "").replace("\r", ""));
		}
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	

}
